package com.nkdx.ws.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.nkdx.ws.constant.CommonConstant;
import com.nkdx.ws.dto.RegionDto;
import com.nkdx.ws.eto.RegionEto;
import com.nkdx.ws.mapper.RegionMapper;
import com.nkdx.ws.service.RegionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 * @author ZZW
 * @date 2022/3/6 12:42 下午
 * @param
 * @return
 */
@Service
public class RegionServiceImpl implements RegionService {

    private RegionMapper regionMapper;

    @Autowired
    public RegionServiceImpl (RegionMapper regionMapper){
        this.regionMapper = regionMapper;
    }

    @Override
    public List<RegionDto> findAllCity() {
        List<RegionDto> list = regionMapper.findAllByType(CommonConstant.REGION_TYPE_CITY);
        return list;
    }

    @Override
    public RegionDto findCityByLikeName(String name) {
        if (StringUtils.isNotBlank(name)){
            return regionMapper.findByLikeNameAndType(name,CommonConstant.REGION_TYPE_CITY);
        }
        return null;
    }

    @Override
    public List<JSONObject> citys() {
        List<RegionDto> list = regionMapper.findAllByType(CommonConstant.REGION_TYPE_CITY);
        ArrayList<JSONObject> citys = new ArrayList<>(list.size());
        for (RegionDto dto : list) {
            JSONObject city = new JSONObject();
            city.put("cityId",dto.getId());
            city.put("cityName",dto.getName());
            citys.add(city);
        }
        return citys;
    }
}
